'use strict';

const { Controller } = require('egg');

class TopicController extends Controller {

    async list() {
        const { ctx, app } = this;
        const pageSize = ctx.query.pageSize ? parseInt(ctx.query.pageSize) : 10;
        const pageNumber = ctx.query.pageNumber ? parseInt(ctx.query.pageNumber) : 1;
        const id = ctx.query.id;
        if (!id) {
            ctx.throw('测评id为空')
        };
        let appraisal = await app.model.Appraisal.findOne({
            where: {
                id
            },
            include: [{
                model: app.model.Topic,
                where: {
                    appraisal_id: id
                },
            }]
        });
        if (!appraisal) {
            return ctx.apiSuccess({
                totalCount: 0,
                currPage: pageNumber,
                list: []
            });
        };
        let list = appraisal.topics.slice((pageNumber - 1) * pageSize, pageNumber * pageSize);
        // 返回用户信息和token
        ctx.apiSuccess({
            totalCount: appraisal.topics.length,
            currPage: pageNumber,
            list
        });
    };


    async addTopic() {
        const { ctx, app } = this;
        ctx.validate({
            appraisal_id: {
                type: 'int',
                required: true,
                desc: "测评id"
            },
            topicname: {
                type: 'string',
                desc: '题目名',
                default: ''
            },
            topicoption: {
                type: 'string',
                required: true,
                desc: '题目选项',
                default: ''
            },
            order: {
                type: 'int',
                required: true,
                desc: '排序值',
            },
        });
        const { appraisal_id, topicname, topicoption, order } = ctx.request.body;
        await app.model.Topic.create({
            appraisal_id,
            topicname,
            topicoption,
            order
        });
        ctx.apiSuccess('添加成功')
    };

    async updateTopic() {
        const { ctx, app } = this;
        ctx.validate({
            id: {
                type: 'int',
                required: true,
                desc: "题目id"
            },
            appraisal_id: {
                type: 'int',
                required: true,
                desc: "测评id"
            },
            topicname: {
                type: 'string',
                desc: '题目名',
                default: ''
            },
            topicoption: {
                type: 'string',
                required: true,
                desc: '题目选项',
                default: ''
            },
            order: {
                type: 'int',
                required: true,
                desc: '排序值',
            },
        });
        const { appraisal_id, topicname, topicoption, order, id } = ctx.request.body;
        let topic = await app.model.Topic.findOne({ id });
        if (!topic) {
            ctx.throw('更新失败,不存在此测评')
        };
        // swiper.image = image;
        // swiper.link = link;
        // swiper.order = order;
        // await swiper.save();
        await app.model.Topic.update({ appraisal_id, topicname, topicoption, order }, {
            where: {
                id
            }
        })
        ctx.apiSuccess('更新成功');
    }

    async deleteTopic() {
        const { ctx, app } = this;
        ctx.validate({
            ids: {
                type: 'all',
                desc: '题目ids'
            }
        });
        let { ids } = ctx.request.body;
        ids = JSON.parse(ids);
        await app.model.Topic.destroy({
            where: {
                id: ids
            }
        });
        ctx.apiSuccess('删除成功');
    }

    async detail() {
        const { ctx, app } = this;
        const { id } = ctx.params;
        let topic = await app.model.Topic.findOne({
            where: {
                id: id
            }
        });
        ctx.apiSuccess(topic);
    }
}

module.exports = TopicController;